package xlj.lib.android.base.toastcompat;

import android.content.Context;
import android.os.Looper;
import android.widget.Toast;


/**
 * Toast管理类，解决toast队列必须显示完后才会关闭的弊端
 */
public class ToastManager {
    private IToast toast;

    public static ToastManager instance;

    private Context context;

    public static ToastManager getInstance() {
        if (instance == null) {
            instance = new ToastManager();
        }
        return instance;
    }

    public void init(Context context) {
        this.context = context;
    }

    private ToastManager() {
    }

    public void showToast(String msg, int time) {

        if (Looper.myLooper() != Looper.getMainLooper()) {
            return;
        }

        if (toast == null) {
            toast = ToastCompat.makeText(context, msg, time);
        } else {
            toast.setText(msg);
            toast.setDuration(time);

        }
        toast.show();
    }

    public void showToast(String msg) {

        if (Looper.myLooper() != Looper.getMainLooper()) {
            return;
        }

        if (toast == null) {
            toast = ToastCompat.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        toast.show();
    }

    public void showToast(String msg,int time,int gravity) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            return;
        }

        if (toast == null) {
            toast = ToastCompat.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setText(msg);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        toast.setGravity(gravity,0,0);
        toast.show();
    }

}
